LeetCode Js-217. Contains Duplicate
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
給予一個整數陣列 nums,如果有任何的值出現最少兩次在陣列中則回傳 true,
如果每個元素都是不同的,則回傳 false。
Example 1:
Input: nums = [1,2,3,1]
Output: true
Solution:
Code:
var containsDuplicate = function(nums) {
let map = {}
for (let i of nums) {
if (map[i]) return true
map[i] = true
}
return false
};
FlowChart:
Example 1
Input: nums = [1,2,3,1]
{ '1': true }
{ '1': true, '2': true }
{ '1': true, '2': true, '3': true }
if (map[1]) = true, return true